import {
  getContentTable,
  editContentRecord
} from '../../api/content'
import {
  VIDEO_LIST,
  VIDEO_CURRENT_ROW,
  VIDEO_DATA_COUNT,
  VIDEO_HISTORY_DATA,
  VIDEO_PAGE_SIZE,
  TASK_VIDEO_HISTORY_DATA
} from '../motations-type'
import videoImg from '@/assets/images/ydkh.jpg'

export default {
  state: {
    videoList: [],
    videoDataCount: null,
    videoPageSize: 20,
    imgPageSize: 8,
    videoHistoryData: [],
    taskVideoHistoryData: [],
    row: {
      id: '',
      img: '',
      ctime: '',
      name: '',
      uptime: '',
      type: '',
      video: '',
      content: '',
      parseURL: ''
    }
  },
  getters: {
    videoList: state => state.videoList
  },
  mutations: {
    [VIDEO_LIST] (state, data) {
      state.videoList = data
    },
    [VIDEO_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [VIDEO_DATA_COUNT] (state, data) {
      state.videoDataCount = data
    },
    [VIDEO_HISTORY_DATA] (state, data) {
      state.videoHistoryData = data
    },
    [VIDEO_PAGE_SIZE] (state, data) {
      state.videoPageSize = data
    },
    [TASK_VIDEO_HISTORY_DATA] (state, data) {
      state.taskVideoHistoryData = data
    }
  },
  actions: {
    async getVideoList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getContentTable(data)
      // console.log(resp)
      let host = resp.data.host
      resp.data.data.rows.map((item) => {
        // if (item.type === 2) {
        newData = [...newData, {
          id: item.id,
          img: item.img,
          host: host,
          ctime: item.ctime,
          name: item.name,
          uptime: item.uptime,
          type: item.type,
          video: item.video,
          content: item.content,
          parseURL: item.parseURL
        }]
        // }
      })
      commit(VIDEO_LIST, newData)
      commit(VIDEO_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.video.videoPageSize) {
        commit(VIDEO_HISTORY_DATA, newData)
      } else {
        commit(VIDEO_HISTORY_DATA, newData.slice(0, rootState.video.videoPageSize))
      }
      if (newData.length < rootState.video.imgPageSize) {
        commit(TASK_VIDEO_HISTORY_DATA, newData)
      } else {
        commit(TASK_VIDEO_HISTORY_DATA, newData.slice(0, rootState.video.imgPageSize))
      }
    },
    async editVideoListData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await editContentRecord(data)
      return resp
    },
    async getVideoListCurrentRow ({ commit, rootState }, row) {
      commit(VIDEO_CURRENT_ROW, row)
    },
    async changeVideoListPage ({ commit, rootState }, index) {
      let data = rootState.video.videoList
      let _start = (index - 1) * rootState.video.videoPageSize
      let _end = index * rootState.video.videoPageSize
      commit(VIDEO_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeVideoListPageSize ({ commit, rootState }, value) {
      commit(VIDEO_PAGE_SIZE, value)
    },
    async changeVideoPage ({ commit, rootState }, index) {
      let data = rootState.video.videoList
      let _start = (index - 1) * rootState.video.imgPageSize
      let _end = index * rootState.video.imgPageSize
      commit(TASK_VIDEO_HISTORY_DATA, data.slice(_start, _end))
    }
  }
}
